package cn.cxq.learning.dynamic_programming;

import org.junit.jupiter.api.Test;

public class IsMatch {

    @Test
    public void test () {
        System.out.println(isMatch("aa","*"));
    }

    public boolean isMatch(String s, String p) {

        int sLen = s.length();
        int pLen = p.length();

        boolean[][] dp = new boolean[sLen + 1][pLen + 1];

        for (int i = 0; i <= sLen; i++) {
            for (int j = 0; j <= pLen; j++) {
                if (j == 0) {
                    dp[i][j] = i == 0;
                } else {
                    if (i > 0) {
                        if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '?') {
                            dp[i][j] = dp[i - 1][j - 1];
                        } else if (p.charAt(j - 1) == '*') {
                            dp[i][j] = dp[i][j - 1] | dp[i - 1][j - 1];
                        }
                    } else {
                        if (p.charAt(j - 1) == '*') {
                            dp[i][j] = dp[i][j - 1];
                        }
                    }
                }
            }
        }

        return dp[sLen][pLen];
    }
}
